package com.sunmouse.toolkit.fragment;

import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.sunmouse.toollib.activity.OnBindInit;
import com.sunmouse.toollib.activity.OnHandleMessageListener;
import com.sunmouse.toollib.adapter.OnChildClickListener;
import com.sunmouse.toollib.utils.JumpUtils;
import com.sunmouse.toollib.utils.PersistentUtils;
import com.sunmouse.toolkit.activity.ContentActivity;
import com.sunmouse.toolkit.adapter.FunAdapter;
import com.sunmouse.toolkit.bean.PageBean;
import com.sunmouse.toolkit.constant.Config;
import com.sunmouse.toolkit.constant.Material;

import java.util.ArrayList;

import lecho.lib.hellocharts.model.Axis;

/**
 * Created by Administrator on 2017/7/25.
 */
public class BaseFragment extends Fragment implements OnBindInit, View.OnClickListener, OnHandleMessageListener ,OnChildClickListener{
    protected final Handler mHandler = new MessageHandler();
    protected Message mMsg;
    private boolean initialized;
    protected int index;
    protected int length;

    protected FragmentManager mManager;
    protected PersistentUtils mPersistentUtils;
    protected Intent mIntent;
    protected Bundle mBundle;
    protected View v;
    protected ArrayList<PageBean> mData;
    protected FunAdapter mAdapter;
    protected PageBean mBean;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initialized = false;
        index = 0;
        length = Material.COLORS.length;
        mPersistentUtils = PersistentUtils.getInstance(getActivity());
        this.mManager=getChildFragmentManager();
    }

    @Override
    public void onStart() {
        super.onStart();
        if (!initialized) {
            initialized = true;
            initNav();
            init();
        }
    }


    /**
     * set title
     */
    protected void setTitle(String s) {
        ActionBar bar = ((AppCompatActivity) getActivity()).getSupportActionBar();
        if (bar != null) bar.setTitle(s);
    }


    protected int getCircleColor() {
        if (index >= length) index = 0;
        return Material.COLORS[index++];
    }

    @NonNull
    protected Axis generateYAxis() {
        Axis yAxis = new Axis();
        yAxis.setName("y(m)");
        yAxis.setLineColor(Material.COLORS[1]);
        yAxis.setTextColor(Material.COLORS[1]);
        yAxis.setHasLines(true);
        return yAxis;
    }

    @NonNull
    protected Axis generateXAxis() {
        Axis xAxis = new Axis();//set coordination
        xAxis.setName("x(m)");
        xAxis.setAutoGenerated(true);//auto generate coordination
        xAxis.setLineColor(Material.COLORS[1]);
        xAxis.setTextColor(Material.COLORS[1]);
        xAxis.setTypeface(Typeface.DEFAULT_BOLD);
        xAxis.setHasLines(true);//multi line mode
//        xAxis.setValues();//set x axis values and flag name
//        xAxis.setInside(true);
//        xAxis.setHasSeparationLine(true);
        return xAxis;
    }

    @Override
    public void init() {

    }

    @Override
    public void initNav() {

    }

    @Override
    public void initDialog() {

    }

    @Override
    public void loadingData(int page) {

    }

    @Override
    public void onClick(View v) {

    }

    @Override
    public void onHandleMessage(Message msg) {

    }

    @Override
    public void onClick(View v, int position) {

    }

    private class MessageHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
//            super.handleMessage(msg);
            onHandleMessage(msg);
        }
    }


    /**
     * jump
     *
     * @param index
     */
    protected void jump(int index) {
        this.mBundle=new Bundle();
        this.mBundle.putInt(Config.Key.INDEX, index);
        JumpUtils.jumpWithParams(getActivity(),ContentActivity.class,mBundle);
    }
}
